﻿//金额输入框onkeyup事件 验证输入金额，允许输入小数点至后2位，其余的都不允许  调用方式： onkeyup="clearNoNum(this)"
function clearNoNum(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符  
    obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是. 
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");//禁止使用输入法(貌似没用啊)
    obj.value = obj.value.replace(/([0-9]+\.[0-9]{2})[0-9]*/, "$1"); //只保留小数点的后2位
    //  /^\d+(\.\d+)?$/   ----验证只能是文字跟小数点     /^\d+(\.\d{2})?$/   只能是数字 保留小数点后2位
}
function clearNoNum_4(obj) {
    obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符  
    obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是. 
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");//禁止使用输入法(貌似没用啊)
    obj.value = obj.value.replace(/([0-9]+\.[0-9]{4})[0-9]*/, "$1"); //只保留小数点的后4位
    //  /^\d+(\.\d+)?$/   ----验证只能是文字跟小数点     /^\d+(\.\d{2})?$/   只能是数字 保留小数点后2位
}
//数字输入框onkeyup事件 验证手机号码，只能输入数字  调用方式： onkeyup="clearNoPhone(this)"
function clearNoPhone(obj) {
    obj.value = obj.value.replace(/[^\d]/g, "");  //清除“数字”以外的字符  
}


//对于银行卡号的控制
function formatInputBankNo(BankNo) {
    if (BankNo.value == "") return;
    var str = BankNo.value.replace(/ /g, "");
    if (/^\d+(\.\d+)?$/.test(str) == false) {
        BankNo.value = "";
        return;
    }

    var account = formatBankNo(BankNo.value);
    if (account != BankNo.value) BankNo.value = account;
}

function formatBankNo(str) {
    var account = new String(str);
    account = account.substring(0, 33); /*帐号的总数, 包括空格在内 */
    if (account.match(".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null) {
        /* 对照格式 */
        if (account.match(".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" +
        ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null) {
            var accountNumeric = accountChar = "", i;
            for (i = 0; i < account.length; i++) {
                accountChar = account.substr(i, 1);
                if (!isNaN(accountChar) && (accountChar != " ")) accountNumeric = accountNumeric + accountChar;
            }
            account = "";
            for (i = 0; i < accountNumeric.length; i++) {    /* 可将以下空格改为-,效果也不错 */
                if (i == 4) account = account + " "; /* 帐号第四位数后加空格 */
                if (i == 8) account = account + " "; /* 帐号第八位数后加空格 */
                if (i == 12) account = account + " ";/* 帐号第十二位后数后加空格 */
                account = account + accountNumeric.substr(i, 1)
            }
        }
    }
    else {
        account = " " + account.substring(1, 5) + " " + account.substring(6, 10) + " " + account.substring(14, 18) + "-" + account.substring(18, 35);
    }
    // console.debug(account);
    return account;
}

//金额用逗号隔开,数字格式化   例如12345 格式化为  12,345.00
function fmoney(s, n) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(),
    r = s.split(".")[1];
    t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("") + "." + r;
}
//还原
function rmoney(s) {
    return parseFloat(s.replace(/[^\d\.-]/g, ""));
}


//getData获取数据的函数
//TotalCount 数据总条数
//PageNumber 当前页码
// PageSize 页码容量
function makePageBar(getData, TotalCount, PageNumber, PageSize) {
    FillPager(".pageIn", {
        TotalCount: TotalCount,
        PageNumber: PageNumber,
        PageSize: PageSize,
        Type: 1,
        Click: getData
    });
    FillPager(".pageIn2", {
        TotalCount: TotalCount,
        PageNumber: PageNumber,
        PageSize: PageSize,
        Type: 2,
        Click: getData
    });

}

function reload() {
    var hrefstr = location.href
    if (hrefstr.indexOf("?") > 0)//包含参数
    {
        location.href = location.href.substring(0, hrefstr.indexOf("?"));
    }
    else {
        location.href = location.href;
    }
}
(function (JQuery) {
    jQuery.extend({
        URL: function () {
            return location.protocol + "//" + location.host + "/";
        },
        BuildingURL: function () {
            // return "http://api.tops001.com/";
            //return "http://testtop.tops001.com:1060/";
            return "http://apinew.beta.tops001.com/";
        },
        FinancialURL: function () {
            //return "http://nwapi.test.tops001.com/";
            return "http://finance.beta.tops001.com/";
            //return "http://apinew.beta.tops001.com/";
        },
        KaKaoCRMURL: function () {
            // return "http://crm.kakao.tops001.com";
            return "http://crmkakao.beta.tops001.com/";
            // return "";
        },
        PageSize: function () {
            var obj = $("selectedPageSize");
            if (obj) {
                $(obj).val();
            }
            return 12;
        },
        AccessToken: function () {
            //8e53506e-4a12-4a57-8b55-db42a3eb1086
            var uk = kk.queryString("uk");
            if (uk.indexOf('?') > -1) {
                uk = uk.split('?')[0];
            }

            if (uk)
                return uk.replace(/-/g, "");

            return uk;
        },
        IsRole: function () {
            return $.AccessToken().replace(/-/g, "") != "49eb1d27-8f65-48b2-b0cc-85ab96f4feff".replace(/-/g, "")
        },
        htmlName: function () {
            return location.pathname.substring(location.pathname.lastIndexOf('/') + 1, location.pathname.indexOf("."));
        },
        SetdataReturn: function (index, isinit) {
            if (isinit) {
                if ($(".tab_nav li")) {
                    $(".tab_nav li").each(function () {
                        console.log($(this).attr('value'));
                        amplify.store($.htmlName() + "_" + $(this).attr('value'), 1);
                        //amplify.store($.htmlName() + "_" + $(this).attr('value') + "_" + $.AccessToken(), 1);
                    });
                } else {
                    amplify.store($.htmlName(), 1);
                }

                return;
            }

            var selectobj = $(".tab_nav li[class=on]");
            if (selectobj.length > 0) {
                amplify.store($.htmlName() + "_" + selectobj.data("state"), index);
            } else {
                amplify.store($.htmlName(), index);
            }

            //amplify.store($.htmlName() + "_" + $(selectobj).attr("value") + "_" + $.AccessToken(), index);
        },
        GettdataReturn: function () {
            var selectobj = $(".tab_nav li[class=on]");
            var val = 1;
            if (selectobj.length > 0) {
                val = amplify.store($.htmlName() + "_" + $(selectobj).data('state'));
                //var val = amplify.store($.htmlName() + "_" + $(selectobj).attr('value') + "_" + $.AccessToken());
                return val == undefined ? 1 : val;
            } else {
                val = amplify.store($.htmlName());
                return val == undefined ? 1 : val;
            }

        }
    })
})($)

kk.config.login.url = "/OAuth/Login";
kk.config.login.target = top.window;

//金额用逗号隔开,数字格式化   例如12345 格式化为  12,345.00
function fmoney(s, n) {

    if (s == undefined || s == null) {
        return 0;
    }
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(),
    r = s.split(".")[1];
    t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("") + "." + r;
}
//还原
function rmoney(s) {
    return parseFloat(s.replace(/[^\d\.-]/g, ""));
}

//将字符串转换为时间格式,适用各种浏览器,格式如2011-08-03 09:15:11
function GetTimeByTimeStr(dateString) {
    var timeArr = dateString.split(" ");
    var d = timeArr[0].split("-");
    if (d.length <= 1) {
        d = timeArr[0].split("/");
    }
    var t = timeArr[1].split(":");
    return new Date(d[0], (d[1] - 1), d[2], t[0], t[1], t[2]);
}

/*
     <summary> 侯涛 创建 V1.0
     </summary>
 */
function TimeFormat(data, format, extendFormat) {
    var entity = {
        IsSuccess: false, Data: "", Year: 0, Month: 0, Day: 0, Hour: 0, Minutes: 0, Seconds: 0,
        toString: function () {
            return "";
        }
    };

    if (data == null || data == undefined || data == "") {
        entity.IsSuccess = false;
        entity.Data = "时间不能为空数据";

        return entity;
    }
    var arrayData = data.split(" ");
    var length = arrayData.length;
    var dates = arrayData[0];
    var times = "";

    if (length > 1) {
        times = arrayData[1];
        if (times.indexOf(":") == -1) {
            times += ":";
        }
    }

    dates = dates.replace(/-/g, "/");
    dates = dates.replace(/\\/g, "/");
    var parseResult = Date.parse(dates + " " + times);

    if (isNaN(parseResult)) {
        entity.IsSuccess = false;
        entity.Data = "时间格式不正确";

        return entity;
    }

    var dateObj = new Date(parseResult);
    entity.Year = dateObj.getFullYear();
    entity.Month = formatNumber((dateObj.getMonth() + 1));
    entity.Day = formatNumber(dateObj.getDate());
    entity.Hour = formatNumber(dateObj.getHours());
    entity.Minutes = formatNumber(dateObj.getMinutes());
    entity.Seconds = formatNumber(dateObj.getSeconds());

    var result = entity.Year + "-" + entity.Month + "-" + entity.Day;

    if (!extendFormat) {
        switch (format) {
            case "HH":
                result += " " + entity.Hour;
                break;
            case "HH:mm":
                result += " " + entity.Hour + ":" + entity.Minutes;
                break;
            case "HH:mm:ss":
                result += " " + entity.Hour + ":" + entity.Minutes + ":" + entity.Seconds;
                break;
            case "yyyy":
                result = entity.Year;
                break;
            case "MM":
                result = entity.Month;
                break;
            case "yyyy-MM":
                result = entity.Year + "-" + entity.Month;
                break;
            case "MM-dd":
                result = entity.Month + "-" + entity.Day;
                break;
            case "MM-dd HH:mm":
                result = entity.Month + "-" + entity.Day + " " + entity.Hour + ":" + entity.Minutes;
                break;
        }
    } else {
        switch (extendFormat) {
            case "HH":
                result = entity.Hour;
                break;
            case "HH:mm":
                result = entity.Hour + ":" + entity.Minutes;
                break;
            case "HH:mm:ss":
                result = entity.Hour + ":" + entity.Minutes + ":" + entity.Seconds;
        }
    }

    entity.IsSuccess = true;
    entity.Data = result;

    entity.toString = function () {
        return result;
    }
    return entity;
}

/*
     <summary> 侯涛 创建 V1.0
     </summary>
 */
function formatNumber(str) {
    var val = parseInt(str);

    if (val < 10) {
        return "0" + val;
    }
    return val;
}

$(function () {
    $(".onlyNumber").keydown(function (event) {
        if ((event.keyCode > 47 && event.keyCode < 58)
            || (event.keyCode == 8) || (event.keyCode == 86)
            || (event.keyCode > 95 && event.keyCode < 106)
            || (event.keyCode == 46)) {
            return true;
        } else {
            return false;
        }
    }).focus(function () {
        this.style.imeMode = 'disabled';   // 禁用输入法,禁止输入中文字符
    }).keyup(function (e) {

        $(this).val($(this).val().replace(/\D/g, ''));
    });
});

function CheckNumberLength(obj) {
    if (event.keyCode == 8) {
        return true;
    }
    if (obj.value.length > 7) {
        return false;
    } else {
        return true;
    }
}

function onlyNumberKeydown(obj, isCheckNumberLen) {
    if (isCheckNumberLen) {
        if (event.keyCode != 8) {
            if (obj.value.length > 7) {
                return false;
            }
        }
    }

    if ((event.keyCode > 47 && event.keyCode < 58)
            || (event.keyCode == 8) || (event.keyCode == 86)
            || (event.keyCode > 95 && event.keyCode < 106)
            || (event.keyCode == 46)) {
        return true;
    } else {
        return false;
    }
}

//金额输入框onkeyup事件 验证输入金额，允许输入小数点至后2位，其余的都不允许  调用方式： onkeyup="clearNoNum(this)"
function clearNoNum(obj) {
    $(obj).bind('input propertychange', function () {
        this.value = this.value.replace(/[^\d\.-]/g, "");
    });

    obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符  
    obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是. 
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");//禁止使用输入法(貌似没用啊)
    obj.value = obj.value.replace(/([0-9]+\.[0-9]{2})[0-9]*/, "$1"); //只保留小数点的后2位
    //  /^\d+(\.\d+)?$/   ----验证只能是文字跟小数点     /^\d+(\.\d{2})?$/   只能是数字 保留小数点后2位
}

function onlyNumberKeyup(obj) {
    $(obj).val($(obj).val().replace(/\D/g, ''));

    //if ($(obj).val().indexOf("-") > -1) {
    //    // alert($(obj).val());
    //    var arraySplit = $(obj).val().split("-");
    //    var result = "";
    //    for (var i = 0; i < arraySplit.length - 1; i++) {
    //        if (result != "") {
    //            result += "-";
    //        }
    //        result += arraySplit[i].replace(/\D/g, '');
    //    }

    //    $(obj).val(result);
    //} else{
    //    $(obj).val($(obj).val().replace(/\D/g, ''));
    //}
}